package com.ojoin.trade.supplyloan_core.web.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.SqlId;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlSupplyPaymentDao;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;

@Repository
public class TbSlSupplyPaymentDaoImpl extends BaseDaoImpl<TbSlSupplyPayment>
		implements TbSlSupplyPaymentDao {

	@Override
	public int deleteByPlatformAccountId(Integer platformAccountId,String rowCreateTime) {
		try {
			Map<String, String> parames = new HashMap<String, String>();
			parames.put("platformAccountId", platformAccountId + "");
			parames.put("rowCreateTime", rowCreateTime);
			return sqlSessionTemplate.delete(
					getSqlName("deleteByPlatformAccountId"), parames);
		} catch (Exception e) {
			throw new DaoException(String.format("根据ID删除对象出错！语句：%s",
					getSqlName(SqlId.SQL_DELETE_BY_ID)), e);
		}
	}

	
	

	@Override
	public long querySupplyPaymentMoney(int platformAccountId, String createDateStart,
			String createDateEnd) {
		Map<String, Object> query = new HashMap<String, Object>();
		List<String> li = new ArrayList<String>();
		// li.add(TbSlSupplyInventory.IN);
		li.add(TbSlSupplyPayment.COMPLETEPAY);
		query.put("settlementStates", li);
		query.put("platformAccountId", platformAccountId + "");
		query.put("createDateStart", createDateStart);
		query.put("createDateEnd", createDateEnd);
		
		Object o = sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT),
				query);
		
		if(null == o){
			return 0 ;
		}else {
			return Long.valueOf(o.toString());
		}
	}


	
	public List<TbSlSupplyPayment> query(
			int platformAccountId, String createDateStart,
			String createDateEnd, String rowCreateTimeStart,
			String rowCreateTimeEnd,String rowUpdateTimeStart,String rowUpdateTimeEnd, String settlementState, Pageable page) {
		Map<String, Object> query = new HashMap<String, Object>();
		
		
		if(!CommonUtils.isEmpty(settlementState)){
			List<String> li = new ArrayList<String>();
			if("newCompletePay".equals(settlementState)){
				li.add(TbSlSupplyPayment.COMPLETEPAY);
			}else if("newWaitPay".equals(settlementState)){
				li.add(TbSlSupplyPayment.WAITPAY);
			}else {
				li.add(settlementState);
			}
			// li.add(TbSlSupplyInventory.IN);
			
			query.put("settlementStates", li);
		}
		
		
		
		query.put("platformAccountId", platformAccountId + "");
		query.put("createDateStart", createDateStart);
		query.put("createDateEnd", createDateEnd);
		
		query.put("rowCreateTimeStart", rowCreateTimeStart);
		query.put("rowCreateTimeEnd", rowCreateTimeEnd);
		
		query.put("rowUpdateTimeStart", rowUpdateTimeStart);
		query.put("rowUpdateTimeEnd", rowUpdateTimeEnd);
		
		return sqlSessionTemplate.selectList(getSqlName("select"), query,getPageBounds(page));
	}

	
	@Override
	public List<TbSlSupplyPayment> queryByUidAndCreateDate(
			int platformAccountId, String createDateStart,
			String createDateEnd, String settlementState, Pageable page) {
		return query(platformAccountId, createDateStart,
				createDateEnd,null,null,null,null,settlementState,page);
	}

	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowCreate(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String settlementState, Pageable page) {
		return query(platformAccountId, null,
				null,rowCreateTimeStart,rowCreateTimeEnd,null,null,settlementState,page);
	}

	@Override
	public long querySupplysettlementMoney(int platformAccountId,
			String createDateStart, String createDateEnd,
			String rowCreateTimeStart, String rowCreateTimeEnd,String settlementState) {
		Map<String, Object> query = new HashMap<String, Object>();
		List<String> li = new ArrayList<String>();
		// li.add(TbSlSupplyInventory.IN);
		if(CommonUtils.isEmpty(settlementState)){
			li.add(TbSlSupplyPayment.WAITPAY);
		}else {
			li.add(settlementState);
		}
		

		query.put("settlementStates", li);
		query.put("platformAccountId", platformAccountId + "");
		query.put("createDateStart", createDateStart);
		query.put("createDateEnd", createDateEnd);
		
		query.put("rowCreateTimeStart", rowCreateTimeStart);
		query.put("rowCreateTimeEnd", rowCreateTimeEnd);
		
		Object o = sqlSessionTemplate.selectOne(getSqlName(SqlId.SQL_SELECT_COUNT),
				query);
		
		if(null == o){
			return 0 ;
		}else {
			return Long.valueOf(o.toString());
		}
	}

	@Override
	public void deleteRepeat(int platformAccountId) {
		sqlSessionTemplate.delete(
				getSqlName("deleteRepeat"), platformAccountId);
		
	}

	@Override
	public String getLastInsertTime(int platformAccountId) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLastInsertTime"), platformAccountId);
	}




	@Override
	public String getLastsettlementDate(int platformAccountId) {
		return sqlSessionTemplate.selectOne(getSqlName("selectLastSettlementDate"), platformAccountId);
	}




	@Override
	public String getLastcreateDate(int platformAccountId,String settlementState) {
		Map<String, Object> query = new HashMap<String, Object>();
		if(!CommonUtils.isEmpty(settlementState)){
			
			List<String> li = new ArrayList<String>();
			li.add(settlementState);
			query.put("settlementStates", li);
		}
			
		query.put("platformAccountId", platformAccountId + "");

		
		return sqlSessionTemplate.selectOne(getSqlName("selectLastcreateDate"), query);
	}


	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowCreateAndCreateDate(
			int platformAccountId, String rowCreateTimeStart,
			String rowCreateTimeEnd, String createDateStart,
			String createDateEnd, String settlementState, Pageable page) {
		return query(platformAccountId, createDateStart,
				createDateEnd,rowCreateTimeStart,rowCreateTimeEnd,null,null,settlementState,page);
	}




	@Override
	public List<TbSlSupplyPayment> queryByUidAndRowUpdate(
			int platformAccountId, String rowUpdateTimeStart,
			String rowUpdateTimeEnd, String settlementState, Pageable page) {
		return query(platformAccountId, null,
				null,null,null,rowUpdateTimeStart,rowUpdateTimeEnd,settlementState,page);
	}

}
